package com.wd.core.spring.support;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.annotation.WebListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.wd.base.utils.RuntimeContext;

/**
 * 扩展Spring的ContextLoaderListener。
 * 将ApplicationContext放到该类的静态成员中，以随时获取。
 * <p>在<code>web.xml</code>文件中，
 * 如果希望在工程中的任意位置取得Spring的ApplicationContext,则使用
 * SpringContexstListener取代ContextLoaderListener
 * 
 * @author xiongwenjun
 */

@WebListener
public class SpringContextListener extends ContextLoaderListener {

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	//
	public void contextDestroyed(ServletContextEvent sce) {

	}
	
	@Override
	public void contextInitialized(ServletContextEvent event) {
		super.contextInitialized(event);
		ServletContext context = event.getServletContext();
		ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
		SpringUtils.setApplicationContext(ctx);
		
		///SystemStartup
		logger.info("Spider is initializing");
		logger.info("Copyright 2006~2015 Wuhan Wodogs Information Technology Co.,Ltd.");
		String realPath = context.getRealPath("/");
		
		RuntimeContext.setRealPath(realPath);
		RuntimeContext.setOs(System.getProperty("os.name"));
		RuntimeContext.setFileSeparator(System.getProperty("file.separator"));

		logger.info("System has been installed at " + realPath);
		logger.info("Spider 3.1 is ready.");
		
		System.out.println("============");
	}
}
